using B.S.RbacData.ErrorCode;
using ContentmanagementBaseWrite.API.Applications.Commands.Questions;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseWrite.API.Controllers
{
    /// <summary>
    /// 问题管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class QuestionsController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 日志记录器
        /// </summary>
        private readonly ILogger<QuestionsController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public QuestionsController(IMediator mediator, ILogger<QuestionsController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 新增常见问题
        /// </summary>
        /// <param name="command">新增常见问题命令</param>
        /// <returns>新增结果</returns>
        [HttpPost("add")]
        public async Task<ApiResult<int>> AddProblem([FromBody] AddNewProblemCommand command)
        {
            try
            {
                _logger.LogInformation("接收到新增常见问题请求，问题标题：{ProblemTitle}", command.ProblemTitle);

                var result = await _mediator.Send(command);

                _logger.LogInformation("常见问题新增请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "常见问题新增请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"常见问题新增异常：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 修改常见问题
        /// </summary>
        /// <param name="command">修改常见问题命令</param>
        /// <returns>修改结果</returns>
        [HttpPut("update")]
        public async Task<ApiResult<bool>> UpdateProblem([FromBody] UpdateProblemCommand command)
        {
            try
            {
                _logger.LogInformation("接收到修改常见问题请求，问题ID：{Id}，问题标题：{ProblemTitle}", 
                    command.Id, command.ProblemTitle);

                var result = await _mediator.Send(command);

                _logger.LogInformation("常见问题修改请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "常见问题修改请求处理异常，问题ID：{Id}", command.Id);
                return new ApiResult<bool>
                {
                    code = ResultEnumCode.fails,
                    message = $"常见问题修改异常：{ex.Message}",
                    data = false
                };
            }
        }

        /// <summary>
        /// 批量删除常见问题
        /// </summary>
        /// <param name="command">批量删除常见问题命令</param>
        /// <returns>删除结果</returns>
        [HttpPost("batch-delete")]
        public async Task<ApiResult<bool>> BatchDeleteProblems([FromBody] BatchDeleteProblemCommand command)
        {
            try
            {
                _logger.LogInformation("接收到批量删除常见问题请求，问题数量：{Count}，ID列表：{@Ids}", 
                    command.Ids?.Count ?? 0, command.Ids);

                var result = await _mediator.Send(command);

                _logger.LogInformation("常见问题批量删除请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "常见问题批量删除请求处理异常");
                return new ApiResult<bool>
                {
                    code = ResultEnumCode.fails,
                    message = $"常见问题批量删除异常：{ex.Message}",
                    data = false
                };
            }
        }
    }
}